package cn.com.headfree.feedback.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.common.dto.Page;

import cn.com.headfree.entity.TFeedBack;
import cn.com.headfree.entity.TSysDictionaryInfo;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.feedback.dao.IFeedbackDao;
import cn.com.headfree.util.DateUtils;

/**  
 * @Title  FeedBackDaoImpl.java   
 * @Package cn.com.headfree.feedback.dao.impl   
 * @Description 意见反馈daoImpl
 * @author yangwang 
 * @date  2020年8月3日 下午2:45:59   
 */
@Repository("feedbackDao")
public class FeedbackDaoImpl extends BaseDaoImpl<TFeedBack> implements IFeedbackDao {

	/**
	 * 新增意见反馈
	 * @param feeback 意见信息
	 *  */
	@Override
	public void insertFeedback(TFeedBack feeback) {
		String sql = "insert into t_feed_back(id,opinion,opinion_time,status,creater_id,creater_name,create_time,updater_id,updater_name,update_time,del_sign,is_valid) values";
		StringBuilder sb = new StringBuilder(sql);
		sb.append("(#{id,jdbcType=VARCHAR},")
		.append("#{opinion,jdbcType=VARCHAR},")
		.append("#{opinion_time},")
//		.append("#{feedback,jdbcType=VARCHAR},")
//		.append("#{feedback_time},")
		.append("#{status,jdbcType=INTEGER},")
		.append("#{creater_id,jdbcType=VARCHAR},")
		.append("#{creater_name,jdbcType=VARCHAR},")
		.append("#{create_time},")
		.append("#{updater_id,jdbcType=VARCHAR},")
		.append("#{updater_name,jdbcType=VARCHAR},")
		.append("#{update_time},")
		.append("#{del_sign,jdbcType=INTEGER},")
		.append("#{is_valid,jdbcType=INTEGER})");
		
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", feeback.getId());
        params.put("opinion", feeback.getOpinion());
        params.put("opinion_time", feeback.getOpinionTime());
//        params.put("feedback", feeback.getFeedback());
//        params.put("feedback_time", feeback.getFeedbackTime());
        params.put("status", feeback.getStatus());
        params.put("creater_id", feeback.getCreaterId());
        params.put("creater_name", feeback.getCreaterName());
		params.put("create_time", feeback.getCreateTime());
		params.put("updater_id", feeback.getUpdaterId());
		params.put("updater_name", feeback.getUpdaterName());
		params.put("update_time", feeback.getUpdateTime());
		params.put("del_sign", 0);
		params.put("is_valid", 0);
        
        this.executeSql(sb.toString(), params);
	}
	
	/**
	 * 更新意见反馈
	 * @param feeback 意见信息
	 *  */
	@Override
	public void updateFeedback(TFeedBack feeback) {
		String sql = "update t_feed_back set ";
		StringBuilder sb = new StringBuilder(sql);
		sb.append("feedback=#{feedback,jdbcType=VARCHAR},")
		.append("feedback_time=#{feedback_time},")
		.append("status=#{status,jdbcType=INTEGER},")
		.append("updater_id=#{updater_id,jdbcType=VARCHAR},")
		.append("updater_name=#{updater_name,jdbcType=VARCHAR},")
		.append("update_time=#{update_time}")
		.append(" where id=#{id,jdbcType=VARCHAR}");
		
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", feeback.getId());
        params.put("feedback", feeback.getFeedback());
        params.put("feedback_time", feeback.getFeedbackTime());
        params.put("status", feeback.getStatus());
		params.put("updater_id", feeback.getUpdaterId());
		params.put("updater_name", feeback.getUpdaterName());
		params.put("update_time", feeback.getUpdateTime());
        
        this.executeSql(sb.toString(), params);
	}
	
	/**
	 * 删除意见反馈
	 * @param user
	 * @param ids 多个id以逗号分隔
	 *  */
	@Override
	public void deleteFeedback(User user,String ids) {
		if(StringUtils.isNotBlank(ids)) {
			String sql = "update t_feed_back set";
			StringBuilder sb=new StringBuilder(sql);
			Map<String,Object> params = new HashMap<String,Object>();
			sb.append(" del_sign=1,")
			.append(" update_time=#{update_time},");
			params.put("update_time", new Date());
			if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
				sb.append(" updater_id=#{updater_id},")
				.append(" updater_name=#{updater_name}");
				params.put("updater_id", user.getFax());
				params.put("updater_name", user.getName());
			}
			sb.append(" where");
			String[] idArr = ids.split(",|;|，|；");
			if((null != idArr)&&(0 < idArr.length)) {
				Boolean bool = false;
				for(int i=0;i<idArr.length;i++) {
					if(StringUtils.isNotBlank(idArr[i])) {
						if(bool) {
							sb.append(" or id=#{id"+i+"}");
						}else {
							sb.append(" id=#{id"+i+"}");
						}
						bool = true;
						params.put("id"+i, idArr[i]);
					}
				}
				if(bool) {
					this.executeSql(sb.toString(), params);
				}
			}
		}
	}
	
	/**
	 * 获取意见反馈
	 * @param id 主键
	 * @return TFeedBack
	 *  */
	@Override
	public TFeedBack getFeedback(String id) {
		String sql = "select distinct id,opinion,opinion_time,feedback,feedback_time,status,creater_id,creater_name,create_time,updater_id,updater_name,update_time";
    	StringBuilder sb = new StringBuilder(sql);
    	sb.append(" from t_feed_back")
    	.append(" where del_sign=0")
    	.append(" and is_valid=0")
    	.append(" and id=?");
    	
    	return this.findUnique(TFeedBack.class, sb.toString(), new Object[] {id});
	}
	
	/**
	 * 获取意见反馈分页信息
	 * @param page 分页
	 * @param feeback 
	 *  */
	@Override
	public void getPage(Page<TFeedBack> page,TFeedBack feeback) {
		String sql = "select distinct id,opinion,opinion_time,feedback,feedback_time,status,creater_id,creater_name,create_time,updater_id,updater_name,update_time";
    	StringBuilder sb = new StringBuilder(sql);
    	sb.append(" from t_feed_back")
    	.append(" where del_sign=0")
    	.append(" and is_valid=0");
    	
    	List<Object> paramList = new ArrayList<Object>();
    	if(null != feeback) {
    		if(StringUtils.isNotBlank(feeback.getCreaterId())) {
    			sb.append(" and creater_id=?");
    			paramList.add(feeback.getCreaterId());
    		}
    		if((null != feeback.getStatus())&&(-1 != feeback.getStatus())) {
				sb.append(" and status = ?");
    			paramList.add(feeback.getStatus());
			}
    		if(null != feeback.getOpinionStartTime()) {
				sb.append(" and opinion_time >= ?");
    			paramList.add(feeback.getOpinionStartTime());
			}
			if(null != feeback.getOpinionEndTime()) {
				sb.append(" and opinion_time <= ?");
    			paramList.add(feeback.getOpinionEndTime());
			}
    		if(null != feeback.getFeedbackStartTime()) {
				sb.append(" and feedback_time >= ?");
    			paramList.add(DateUtils.getStartTimeOfDay(feeback.getFeedbackStartTime()));
			}
			if(null != feeback.getFeedbackEndTime()) {
				sb.append(" and feedback_time <= ?");
    			paramList.add(DateUtils.getEndTimeOfDay(feeback.getFeedbackEndTime()));
			}
    	}
    	sb.append(" order by create_time desc");
    	
    	this.findPageBySql(TFeedBack.class, sb.toString(), page, paramList.toArray());
	}
	
	/**
	 * 获取字典列表
	 * @param value
	 * @return List<TSysDictionaryInfo>
	 *  */
	@Override
	public List<TSysDictionaryInfo> getDictionarys(String value){
		String sql = "select id,name,value from t_sys_dictionary_info ";
    	StringBuilder sb = new StringBuilder(sql);
    	sb.append(" where del_sign=0")
    	.append("  and p_id=(select id")
    	.append("  from t_sys_dictionary_info")
    	.append(" where del_sign=0")
    	.append(" and value=?)");
    	
    	return this.findAllBySQL(TSysDictionaryInfo.class, sb.toString(), null, false, new Object[] {value});
	}
	
}
